import userService from '@/service/user.js';
import $store from '@/store/index.js';
import $go from './go.js';
module.exports = {
	isEmpty(clientid){
		return  !clientid || typeof clientid == "undefined" || clientid=='null'  || clientid=='undefined' 
	},
	register(token){
		// #ifdef APP-PLUS 
		//清除角标
		plus.runtime.setBadgeNumber(0);
		
		this.getClientId(token);

		// audioPlay('需要播报的文字');语音播报
		plus.push.addEventListener("receive", function(msg) {
			if (msg.aps) { // Apple APNS message  
				//APNS下发的消息，应用在前台  苹果后台通知
				plus.push.createMessage(msg.content, msg.payload, {
					title: msg.title
				}, false);
				// uni.showModal({
				// 	title: 'APNS下发的消息  ' + JSON.stringify(msg)
				// })
			} else if (msg.type != "receive") { // 特殊payload标识本地创建的消息  
				// uni.showModal({
				// 	title: '特殊payload标识本地创建的消息  ',
				// 	content:JSON.stringify(msg)
				// })

				//本地创建的消息，通常不需要处理    
				//注意：不要在这种情况下再此调用plus.push.createMessage，从而引起循环创建本地消息    
			} else {
				//接收到在线透传消息  
				plus.push.createMessage(msg.title, msg.payload, {
					title: msg.title,
				}, false);
			}

		}, false);

		//app杀掉触发click
		plus.push.addEventListener("click", function(msg) {
			let clent = uni.getSystemInfoSync().platform;

			if (clent == 'ios') { //如果是IOS  
				var payload = JSON.parse(decodeURIComponent(msg.payload));
				if (payload.type == 'orders') {
					let url = '/pages/order/detail?';
					if (payload.params.length !== 0) {
						for (let i in payload.params) {
							url = url + payload.params[i].key + '=' + payload.params[i].value + '&'
						}

						uni.navigateTo({
							url: url
						})
					}
				}else{
					$go.page(payload);
				}
			} else if (clent == 'android') { //如果是Android，收到playload均是是JSON字符串，需要转为JSON对象    
				//payload = JSON.parse(decodeURIComponent(msg.payload));
				let payload = msg.payload;
				if (payload.type == 'orders'){
					let url = '/pages/order/detail?';
					for (let i in payload.params) {
						url = url + payload.params[i].key + '=' + payload.params[i].value + '&'
					}
					if (payload.params.length !== 0) {
						uni.navigateTo({
							url: url
						})
					}
				}else{
					$go.page(payload);
				}
				


			}

		}, false);

		// #endif
	
	},
	
	getClientId(token){
		let that = this;
		// #ifdef APP-PLUS  
		if (token) {
			let clientid = plus.push.getClientInfo().clientid;
			if (this.isEmpty(clientid)) {
				plus.push.getClientInfoAsync((info) => {
					if (this.isEmpty(clientid)) {
						clientid = info.clientid;
						if (!this.isEmpty(clientid)) that.bind(clientid);
					}

				}, err => {});
				setTimeout(() => {
					if (this.isEmpty(clientid)) {
						clientid = plus.push.getClientInfo().clientid;
						if (!this.isEmpty(clientid)) that.bind(clientid);
					}
				}, 4000);
			} else {
				that.bind(clientid);
			}

		}
		// #endif
	},	
	bind(clientid) { //更新token/登陆
		console.log('clientId',clientid);
		if (this.isEmpty(clientid)){
			userService.bindPush(clientid).then((res) => {
				console.log('绑定成功', res)
		 
			}).catch(err => {});
		}
	 },

}
